import threading
import time

# 初始化信号量
driver_semaphore = threading.Semaphore(1)
conductor_semaphore = threading.Semaphore(1)

class Driver(threading.Thread):
    def run(self):
        global stop_flag
        while not stop_flag:
            driveToNextStop()
            conductor_semaphore.acquire()
            stopTheBus()
            closeTheDoor()
            conductor_semaphore.release()

class Conductor(threading.Thread):
    def run(self):
        global stop_flag
        while not stop_flag:
            conductor_semaphore.acquire()
            sellTicket()
            conductor_semaphore.release()
            openTheDoor()
            time.sleep(1)

def driveToNextStop():
    #到下一站
    print("正在行驶到下一站...")
    time.sleep(3)

def stopTheBus():
    # 停车
    print("到达站点，正在停车...")
    time.sleep(3)

def closeTheDoor():
    # 关车门
    print("关门，准备出发...")
    time.sleep(3)

def sellTicket():
    # 售票
    print("售票中...")
    time.sleep(3)

def openTheDoor():
    # 开车门
    print("开门，欢迎下车...")
    time.sleep(3)


# 初始化线程并启动
stop_flag = False
driver = Driver()
conductor = Conductor()
driver.start()
conductor.start()

# 等待公交车行驶结束
time.sleep(20)
stop_flag = True
driver.join()
conductor.join()
